#include "label.h"

#define Rotate_left(a,b) (((unsigned short)a)<<(b%16))|(((unsigned short)a)>>(16-(b%16)))

sLabel::sLabel():   strVal( "" ){
    update();
}
sLabel::sLabel(string* str): strVal( *str ){
    update();
}
sLabel::sLabel(char* str):  strVal( str ){
    update();
}
sLabel::sLabel(sLabel* lbl):    strVal( lbl->strVal ){
    update();
}
sLabel::~sLabel(){}

void sLabel::update(){
    unChkSum = sLabel::calcChkSum( (char*)strVal.c_str() );
}
unsigned short sLabel::getChkSum(){
    return unChkSum;
}

unsigned short sLabel::calcChkSum( char* inStr ){
    unsigned int i = 0;
    unsigned short out= 0;
    while ( inStr[i] ){
        out ^= Rotate_left( inStr[i], i );
        i++;
    }
    return out;
}

bool sLabel::operator==(sLabel &inLab){
    if( unChkSum != inLab.getChkSum() ) return false;
    if( strcmp( strVal.c_str(), inLab.strVal.c_str() ) == 0 ) return true;
    return false;
}
bool sLabel::operator==(string &inStr){
    unsigned short stackSum = sLabel::calcChkSum( (char*)inStr.c_str() );
    if( unChkSum != stackSum ) return false;
    if( strcmp( strVal.c_str(), inStr.c_str() ) == 0 ) return true;
    return false;
}
bool sLabel::operator==(char* inStr){
    unsigned short stackSum = sLabel::calcChkSum( inStr );
    if( unChkSum != stackSum ) return false;
    if( strcmp( strVal.c_str(), inStr ) == 0 ) return true;
    return false;
}
